package com.xiangty.controller;

import com.xiangty.bean.User;
import com.xiangty.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

@RestController
public class HelloController {

    @Autowired
    UserMapper userMapper;

    @GetMapping("/hello")
    public String hello() {
        return "Hello...";
    }

    @GetMapping("/user")
    public User user() {
        User user = userMapper.Sel(1);
        return user;
    }

    /**
     * 生产springbatch的测试数据
     *
     * @throws IOException
     */
    @GetMapping("/createSpringBatchData")
    private String createSpringBatchData() throws IOException {
        File file = new File("D:\\springbatchdemo\\", "data.txt");
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        FileOutputStream out = new FileOutputStream(file);
        String a = "";
        // 给文件中生产30万条数据
        long beginTime = System.currentTimeMillis();
        System.out.println("开始时间：【 " + beginTime + " 】");
        for (int i = 1; i <= 300000; i++) {
            a = "custNo_" + i + ",applicationNo_" + i + ",value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11,value12,value13,value14,value15,value16,value17,value18,value19,value20\n";
            out.write(a.getBytes());
            out.flush();
        }
        out.close();
        System.out.println("总共耗时：【 " + (System.currentTimeMillis() - beginTime) + " 】毫秒");
        return "文件生成完成...";
    }

}
